package org.hzy.test1;

import java.util.Iterator;
import java.util.List;

import javassist.convert.Transformer;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.transform.Transformers;
import org.hzy.entity.Dept;
import org.hzy.entity.Emp;

public class SQLQueryTest {

	/**
	 * @param args
	 */

	static Configuration con = new Configuration().configure();
	static SessionFactory fac = con.buildSessionFactory();
	static Session sess = fac.getCurrentSession();

	static void ts1(String nativesql) {
		Transaction tran = sess.beginTransaction();

//		SQLQuery squer = sess.createSQLQuery(nativesql).addScalar("deptno", Hibernate.INTEGER)
//		.addScalar("dname").addScalar("loc");
		SQLQuery squer = sess.createSQLQuery(nativesql);
		List<Object> list=squer.list();
		
		System.out.println(list.size());
		
		Iterator it=list.iterator();
		while(it.hasNext()){
			Object[] obj=(Object[]) it.next();
			System.out.println(obj[0]);
		}
		tran.commit();
	}
	
	static void ts2(String nativesql) {
		Transaction tran = sess.beginTransaction();

		SQLQuery squer = sess.createSQLQuery(nativesql).addEntity(Dept.class);
		
		List<Dept> list=squer.list();
		
		System.out.println(list.size());
		
		Iterator it=list.iterator();
		while(it.hasNext()){
			Dept d= (Dept) it.next();
			System.out.println(d.getDeptno());
		}
		tran.commit();
	}
	
	static void ts3(String nativesql) {
		Transaction tran = sess.beginTransaction();

		SQLQuery squer = sess.createSQLQuery(nativesql).addEntity("e",Emp.class);

		List<Emp> list=squer.list();
		
		System.out.println(list.size());
		
		Iterator it=list.iterator();
		while(it.hasNext()){
			Object[] obj=(Object[]) it.next();
			System.out.println(obj[0]);
		}
		tran.commit();
	}
	
/*	static void ts4(String nativesql) {
		Transaction tran = sess.beginTransaction();

		List<Dept> list = sess.createSQLQuery(nativesql).setResultTransformer(Transformers.aliasToBean(Dept.class)).list();

		
		System.out.println(list.size());
		
		Iterator it=list.iterator();
		while(it.hasNext()){
			Dept d= (Dept) it.next();
			System.out.println(d.getDeptno());
		}
		tran.commit();
	}*/
	
	static void ts5() {
		Transaction tran = sess.beginTransaction();
		
		Query query=sess.getNamedQuery("test2");
		

		List list=query.list();
//		
//		System.out.println(list.size());
		
//		Iterator it=list.iterator();
//		while(it.hasNext()){
//			Dept d= (Dept) it.next();
//			System.out.println(d.getDeptno());
//		}
		tran.commit();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		ts1("select * from scott.dept");//查出来都是ojbect[]
		
//		ts1("select deptno,dname from scott.dept");
		
		
	
		
		
//		ts2("select d.deptno,d.dname,d.loc from scott.dept d");//查出来是对象
		
//		ts2("select deptno,dname,loc from scott.dept");
		
//		ts2("select d.deptno,d.dname,d.loc from scott.dept d");
		
//		ts3("select {d.*},{e.*} from scott.dept d,scott.emp e where d.deptno=e.deptno"); //查两列无效
		
//		ts3("select e.empno,e.ename,e.job,e.mgr,e.hiredate,sal,comm,e.deptno from scott.emp e");
		
//		ts3("select e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno from scott.emp e,scott.dept d where e.deptno=d.deptno");
		
		ts5();
		
		
		
	}

}
